/*
 * ==================================================
 * 项目开发者：JerryStark
 * 开发者Email：4771007@qq.com
 * 开发者QQ:4771007
 * 开发范围：web，wap，android,ios,osx or win or linux 应用
 * ==================================================
 */
package lottery;

import RTPower.RTdate;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author jerry
 */
public class ScanHtmlData {

    /**
     * 抓取规则的表配置集合
     * <br>class
     * <br>tag
     * <br>id
     * <br>key
     */
    private JsonObject GetTableConf;

    /**
     * 抓取的行配置集合
     * <br>tag
     * <br>line
     */
    private JsonObject GetRowConf;

    /**
     * 抓取的单元格配置集合
     * <br>tag
     */
    private JsonObject GetCellConf;

    /**
     * 抓取的key的配置集合
     * <br>最终具体抓取的映射
     * <br>orderid
     * <br>numbers
     * <br>number_split_tag
     * <br>time
     * <br>time_format
     */
    private JsonObject GetKeyConf;

    /**
     * 要解析的字符串
     */
    private String ScanString;

    private Document HtmlData;

    /**
     * 最终解析完毕的数据
     */
    private JsonObject NewData = new JsonObject();

    /**
     * 接收扫描的字符和规则
     * <br>对规则进行拆分,对字符进行解析DOC
     *
     * @param scan_string
     * @param ScanRule
     */
    public ScanHtmlData(String scan_string, JsonObject ScanRule) {

        ScanString = scan_string;

        //规则进行拆分
        GetTableConf = ScanRule.getAsJsonObject("table_conf");
        GetRowConf = ScanRule.getAsJsonObject("row_conf");
        GetCellConf = ScanRule.getAsJsonObject("cell_conf");
        GetKeyConf = ScanRule.getAsJsonObject("key_conf");
    }

    /**
     * 开始正式解析
     *
     * @return
     */
    public JsonObject Start() {

        //把Strng解析成Html
        ChanageStringToDoc();

        //没有数据就终止
        if (HtmlData == null) {
            return NewData;
        }

        //开始处理
        StartScan();
        return NewData;
    }

    /**
     * 把string数据转化成htmldoc
     */
    private void ChanageStringToDoc() {
        HtmlData = Jsoup.parse(ScanString);
    }

    /**
     * 正式是开始处理解析数据
     */
    private void StartScan() {
        Element table_doc = ScanRuleTable();
        Element row_doc = ScanRuleRow(table_doc);
        Elements cell_docs = ScanRuleCell(row_doc);
        NewData = ScanRuleKey(cell_docs);
    }

    /**
     * 按照规则得到表数据
     * <br>
     *
     * @return
     */
    private Element ScanRuleTable() {
        //检查的优先级为 id class tag  ,key一般是json使用的这里不用
        String table_class = GetTableConf.get("class").getAsString();
        String table_tag = GetTableConf.get("tag").getAsString();
        String table_id = GetTableConf.get("id").getAsString();
        
        //以上的tag class 是获取多个的,所以配合key 来定位
        int table_key =0;
        if(!GetTableConf.get("key").getAsString().equals("")){
             table_key = GetTableConf.get("key").getAsInt();
        }
        

        Element tabel_doc = null;
        if (!table_id.equals("")) {
            tabel_doc = HtmlData.getElementById(table_id);
        }
        //class存在则
        if (!table_class.equals("")) {
            Elements class_docs = HtmlData.getElementsByClass(table_class);
            tabel_doc = class_docs.get(table_key);

        }
        //tag存在则
        if (!table_tag.equals("")) {
            Elements tag_docs = HtmlData.getElementsByTag(table_tag);
            tabel_doc = tag_docs.get(table_key);
        }
 
        return tabel_doc;
    }

    /**
     * 查找行特征得到数据一行
     *
     * @param table_doc
     * @return
     */
    private Element ScanRuleRow(Element table_doc) {
        String row_tag = GetRowConf.get("tag").getAsString();
        int row_line = GetRowConf.get("line").getAsInt();
        //提取行,先进行行标签确定
        Elements row_docs = table_doc.getElementsByTag(row_tag);
        Element row_doc = row_docs.get(row_line);

        return row_doc;
    }

    /**
     * 这里返回的是多行的单元集合
     *
     * @param row_doc
     * @return
     */
    private Elements ScanRuleCell(Element row_doc) {
        //根据行,获得单元的额切换组
        String row_tag = GetCellConf.get("tag").getAsString();

        Elements cell_docs = row_doc.getElementsByTag(row_tag);
        return cell_docs;
    }

    private JsonObject ScanRuleKey(Elements cell_docs) {
        //最终键值对应获得最终数据
        //先解析配置
        int orderid_id = GetKeyConf.get("orderid").getAsInt();
        int numbers = GetKeyConf.get("numbers").getAsInt();
        String number_split_tag = GetKeyConf.get("number_split_tag").getAsString();
        int time = GetKeyConf.get("time").getAsInt();
        String time_format = GetKeyConf.get("time_format").getAsString();

        //开始键值对应读取数据
        String use_orderid = cell_docs.get(orderid_id).text();
        String use_numbers = cell_docs.get(numbers).text();
        String use_time = cell_docs.get(time).text();

        //最终准备返回的数据
        JsonObject back_data = new JsonObject();

        //检查格式化
        if (!number_split_tag.equals("")) {
            String[] sp_numbers = use_numbers.split(number_split_tag);
            use_numbers = new Gson().toJson(sp_numbers);
        }
        //时间格式检查
        if (!time_format.equals("")) {
            use_time = String.valueOf(RTdate.TimeToStemp(use_time, time_format));
        }

        back_data.addProperty("orderid", use_orderid);
        back_data.addProperty("numbers", use_numbers);
        back_data.addProperty("time", use_time);

        return back_data;
    }
}
